﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using StartupTaskRunner;
using StartupTaskRunner.Configuration;

namespace StartupTaskRunner.Configuration
{
    public partial class ConfigType
    {
        private static ConfigType _configuration;

        /// <summary>
        /// Reads the configurtion file and instantiates a new ConfigType object
        /// </summary>
        /// <param name="configFile"></param>
        /// <returns></returns>
        internal static ConfigType ReadConfigurationFromFile(string configFile)
        {
            if (_configuration == null)
            {
                XmlSerializer xs = new XmlSerializer(typeof(ConfigType));
                using (FileStream fs = new FileStream(configFile, FileMode.Open))
                {
                    _configuration = xs.Deserialize(fs) as ConfigType;
                }
            }
            return _configuration;
        }

        internal static ConfigType ReadConfiguration(string configurationContent)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(configurationContent);
            var memoryStream = new MemoryStream(byteArray);

            XmlSerializer xs = new XmlSerializer(typeof(ConfigType));
            _configuration = xs.Deserialize(memoryStream) as ConfigType;

            return _configuration;
        }
        /// <summary>
        ///  Provides access to the configuration
        /// </summary>
        /// <returns></returns>
        internal static ConfigType GetConfiguration()
        {
            return _configuration;
        }

        /// <summary>
        /// Provides access to BlobStorage
        /// </summary>
        /// <returns></returns>
        internal static BlobStorageType GetBlobStorage()
        {
            return _configuration.BlobStorage;
        }

        public bool DevelopmentMode { get; set; }

    }
}